Skip to content

[experiment] Menu2 shared styles as layered styled components - #2

Draft
siriwatknp wants to merge 1 commit into
LukasTy:menu-rfc-playgroundfrom
siriwatknp:menu2-styled-poc
Draft

siriwatknp wants to merge 1 commit into
LukasTy:menu-rfc-playgroundfrom
siriwatknp:menu2-styled-poc

Conversation

@siriwatknp

Copy link
Copy Markdown

Companion PoC for mui#48823, targeting its branch. Not intended to merge as-is — it demonstrates an alternative to the shared style modules for discussion.

What it does

Replaces the exported style objects/functions (Menu/menuStyles.js, MenuItem/menuItemStyles.js, Unstable_Menu2/menu2SharedStyles.ts — all three deleted) with nameless shared styled components:

  • MenuItem/MenuItemBase.tsx — all shared item styles + variants, on ButtonBase
  • Unstable_Menu2/Menu2ItemBase.tsx — Menu2 delta (div resets, disabled pointer-events)
  • Unstable_Menu2/Menu2PopupSlotBases.tsx — positioner/paper/list bases shared by Menu2Popup and Menu2SubmenuPopup
  • Unstable_Menu2/Menu2IndicatorBase.tsx — shared by both indicators

Each family wraps a base with its own name/slot, so theme keys and classes stay isolated:

const Menu2ItemRoot = styled(Menu2ItemBase, {
  name: 'MuiMenu2Item',
  slot: 'Root',
  overridesResolver: menu2ItemOverridesResolver,
  shouldForwardProp: (prop) => rootShouldForwardProp(prop) || prop === 'ownerState',
})<{ ownerState: Menu2ItemOwnerState }>({});

This removes the getMenuItemRootStyles(theme, classes, options) pattern: no class-map parameter, no focusVisibleClass/disabledPointerEvents option flags, no memoTheme at call sites. Classic MenuItem.js/Menu.js end up close to their master state.

How the class parameterization was resolved

  • selected/disabled state classes are global (Mui-selected/Mui-disabled) — identical for every family.
  • The only family-specific selector was highlighted. The base styles it through one dual selector: &.Mui-focusVisible (classic, from ButtonBase focus tracking) and &[data-highlighted] (Base UI attribute). At most one matches per family. This is the main contract question: the highlight visual now keys on a Base UI data-* attribute rather than the family class.

Verified

  • tsc clean, ESLint clean, 350 unit tests pass (jsdom scope), including the describeConformance styleOverrides/variant assertions that exercise the ownerState forwarding.
  • Pigment CSS: verified the transform (0.0.31, via @wyw-in-js/transform) on a fixture mirroring this exact shape — cross-file layered composition, function-prop variants, and the dual selector all extract correctly. One caveat found: a zero-argument styled(X, opts)() crashes the Pigment processor, so the wrappers use ({}).

Trade-offs to weigh

  • Named wrappers over a variant-carrying base must forward ownerState (shouldForwardProp) — forgetting it silently disables dense/divider/gutters for that family. Conformance testVariantProps catches the dense case.
  • The [data-highlighted] styling dependency vs the RFC's "Mui-* classes are the public contract" rule needs an explicit decision.
  • Browser/visual (Argos) parity not yet run.

Also observed while extracting: once maxHeight diverged, the classic and Menu2 popups genuinely share only WebkitOverflowScrolling: touch — the real popup-style sharing is between Menu2Popup and Menu2SubmenuPopup. The classic Menu paper/list styles are inlined back into Menu.js.

Share menu item, popup slot, and indicator styles through nameless styled
bases instead of exported style objects and functions. Each family wraps a
base with its own theme name and slot, so theme keys and classes stay
isolated. Removes menuStyles.js, menuItemStyles.js, and menu2SharedStyles.ts.

Named wrappers forward ownerState so the base variants still match, and use
({}) instead of a zero-argument call: Pigment CSS 0.0.31 crashes on
styled(X, opts)(). The shared item base styles the highlight through
.Mui-focusVisible (classic) and [data-highlighted] (Base UI) in one selector.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant